import java.util.*;

/**
 * @author zhangshuai
 * @version V1.0
 * @Project Java2_26
 * @Package PACKAGE_NAME
 * @Description:
 * @Date 2025/5/16 16:23
 */

public class SortTest {

    public static void main(String[] args) {
        //Collections的方法
        //反转
/*        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println("list"+list);
        Collections.reverse(list);//反转
        System.out.println("list反转"+list);
        //随机排序
        Collections.shuffle(list);//随机排序
        System.out.println("list随机排序"+list);
        //替换
        Collections.replaceAll(list, 1, 100);
        System.out.println("list替换"+list);
        Collections.swap(list, 0, 1);//交换
        System.out.println("list交换"+list);
        //排序   自然排序  升序
        Collections.sort(list);//排序
        //降序
        Collections.sort(list, Collections.reverseOrder());


        //自定义排序 new Comparator<Integer>() 匿名内部类
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //o1 - o2 升序  o2 - o1 降序
                return o2 - o1;
            }
        });
        System.out.println("list排序"+list);
        //基本类型+String都可以进行自然排序
        //String 按照字典序排序
        List<String> list1 = new ArrayList<>();
        list1.add("ac");
        list1.add("ba");
        list1.add("cd");
        list1.add("dd");
        list1.add("db");
        System.out.println("list1"+list1);
        Collections.sort(list1);
        System.out.println("list1排序"+list1);
        //对象是否能进行排序？

        List<Stuent> list2 = new ArrayList<>();
        list2.add(new Stuent("zs", 18, 90));
        list2.add(new Stuent("ls", 25, 90));
        list2.add(new Stuent("ww", 18, 80));
        list2.add(new Stuent("zl", 21, 70));
        list2.add(new Stuent("z4", 51, 75));
        list2.add(new Stuent("ss", 51, 70));
        System.out.println("list2"+list2);
        //内部比较器 实现的是Comparator接口
        Collections.sort(list2);*/


       /* Collections.sort(list2, new Comparator<Stuent>() {
            @Override
            public int compare(Stuent o1, Stuent o2) {
                //o1 - o2 升序  o2 - o1 降序  年龄的升序排序
               *//* return o1.getAge() - o2.getAge();*//*
                //需求 分数按照降序排序，分数相同的情况下，按照name升序排序
                if (o1.getScore() == o2.getScore()) {
                    return o1.getName().compareTo(o2.getName());
                }
                return o2.getScore() - o1.getScore();


            }
        });*/
   /*     System.out.println("list2排序"+list2);*/

        //内部比较器 实现的是Comparable接口
        //外部比较器 实现的是Comparator接口

        //TreeSet 底层是红黑树，自动排序 TreeMap 底层是红黑树，自动排序
        //需要年龄的升序排序，年龄相同的情况下，按照name升序排序
        TreeSet<Stuent> treeSet = new TreeSet<>(new Comparator<Stuent>() {
            @Override
            public int compare(Stuent o1, Stuent o2) {
                //o1 - o2 升序  o2 - o1 降序  年龄的升序排序
                if (o1.getAge() == o2.getAge()) {
                    if (o1.getScore() == o2.getScore()) {
                        return o1.getName().compareTo(o2.getName());
                    }
                    return o1.getScore() - o2.getScore();
                }
              return o1.getAge() - o2.getAge();
            }
        });

        treeSet.add(new Stuent("zs", 18, 90));
        treeSet.add(new Stuent("ls", 25, 90));
        treeSet.add(new Stuent("ww", 38, 80));
        treeSet.add(new Stuent("zl", 21, 70));
        treeSet.add(new Stuent("z2", 21, 75));
        treeSet.add(new Stuent("ss", 21, 70));
        System.out.println("treeSet"+treeSet);
        //TreeMap 根据key自动排序
        TreeMap<Stuent, Object> objectObjectTreeMap = new TreeMap<>(new Comparator<Stuent>() {
            @Override
            public int compare(Stuent o1, Stuent o2) {
                    return o1.getScore()-o2.getScore();
            }
        });
        //key 是唯一的
        objectObjectTreeMap.put(new Stuent("ss", 21, 90), new Stuent("ss", 21, 90));
        objectObjectTreeMap.put(new Stuent("zs", 51, 80), new Stuent("zs", 51, 80));
        objectObjectTreeMap.put(new Stuent("zs", 51, 80), new Stuent("ks", 65, 70));
        objectObjectTreeMap.put(new Stuent("zs", 51, 80), new Stuent("as", 32, 80));
        objectObjectTreeMap.put(new Stuent("zs", 51, 80), new Stuent("bs", 21, 70));

        //用TreeMap实现一个学生成绩管理排序，成绩按照降序排序，成绩相同的情况下，按照name升序排序，内部或者外部


    }
}
class Stuent implements Comparable<Stuent>  {
    private String name;
    private int age;
    private int score;

    public Stuent(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Stuent{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }


    @Override
    public int compareTo(Stuent o) {
        //this在前 升序 this在后 降序
        return this.getAge() - o.getAge();
    }
}